import { createWebHistory, createRouter } from 'vue-router'
/* Layout */
import Layout from '../layout/index.vue'
// 公共路由
export const constantRoutes = [
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('../views/Home.vue'),
        name: 'Home'
      }
    ]
  },
  {
    path: '/aboutus',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('../views/AboutUs.vue'),
        name: 'aboutus'
      }
    ]
  },
  {
    path: '/productList',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('../views/ProductList.vue'),
        name: 'productList'
      }
    ]
  },
  {
    path: '/productDetail/:id',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('../views/ProductDetail.vue'),
        name: 'productDetail'
      }
    ]
  },
  {
    path: '/shop',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('../views/Shop.vue'),
        name: 'Shop'
      }
    ]
  },
  {
    path: '/login',
    component: () => import('../views/Login.vue'),
    name: 'Login',
  },
  {
    path: '/register',
    component: () => import('../views/Register.vue'),
    name: 'Register',
  },
  {
    path: '/:pathMatch(.*)*',
    //访问主页的时候 重定向到index页面
    component: () => import('../views/404.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes: [...constantRoutes],
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

export default router
